// program/pages/admin/plugins/tencentCloud/order/list.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentType: 1,
        page: 1,
        search: {
            deal_names: '',
            sales_uin: '',
            start_time: '',
            end_time: ''
        },
        salerList: [{
            sales_name: '全部业务员',
            sales_uin: ''
        }],
        salerIdx: 0,
        orderList: [],
        newOrderList: []
    },


    bindChangeCurrentType(e) {
        if (e.currentTarget.dataset.current === 2) {
            const date = new Date();
            const y = date.getFullYear();
            const m = date.getMonth();
            const d = date.getDate();
            this.setData({
                ['search.start_time']: app.utils.parseTime(new Date(y, m, d - 2).getTime(), '{y}-{m}-{d}'),
                ['search.end_time']: app.utils.parseTime(date.getTime(), '{y}-{m}-{d}'),
            })
        }
        this.setData({
            currentType: e.currentTarget.dataset.current,
        })
        this.loadData(true)
    },
    bindPickerChange: function (e) {
        this.setData({
            salerIdx: e.detail.value
        })
        this.loadData(true)
    },
    bindSearchInput(e) {
        this.setData({
            ['search.deal_names']: e.detail.value
        })
    },
    bindDoSearch() {
        this.loadData(true);
    },
    bindStartTimeChange: function (e) {
        this.setData({
            ['search.start_time']: e.detail.value
        })
    },
    bindEndTimeChange: function (e) {
        this.setData({
            ['search.end_time']: e.detail.value
        })
    },
    bindPayCustomerOrder(e) {
        let that = this;
        app.showConfirm('提示', '是否代付该客户订单', true, () => {
            let data = {
                point_url: app.server.adminApi.doPayTencentCouldCutomerOrder,
                owner_uin: e.currentTarget.dataset.owner,
                deal_name: e.currentTarget.dataset.order
            }
            app.server.getConnect(data).then(function (res) {
                wx.hideLoading();
                wx.showToast({
                    title: '代付成功',
                })
                that.loadData(true);
            }, function (res) {
                console.log(res);
                wx.hideLoading();
            });
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let date = new Date();
        let y = date.getFullYear();
        let m = date.getMonth();
        this.setData({
            ['search.start_time']: app.utils.parseTime(new Date(y, m, 1).getTime(), '{y}-{m}-{d}'),
            ['search.end_time']: app.utils.parseTime(date.getTime(), '{y}-{m}-{d}'),
            clientUin: options.clientUin || ''
        })
        this.loadSalerList();
    },

    loadData(reset) {
        this.loadOrderList(reset);
    },

    loadSalerList() {
        let that = this;
        let data = {
            point_url: app.server.adminApi.getTencentCouldSalerList
        }
        app.server.getConnect(data).then(function (res) {
            that.setData({
                salerList: that.data.salerList.concat(res.data)
            })
            if (res.data.length > 0) {
                that.loadData(true);
            }
        }, function (res) {
            console.log(res);
            wx.hideLoading();
        });
    },

    loadOrderList: function (reset) {
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        if (reset) {
            this.setData({
                orderList: [],
                newOrderList: [],
                page: 1,
            })
        }
        var that = this;
        var data = {
            point_url: app.server.adminApi.getTencentCouldCustomerOrderListHistroy,
            page_no: this.data.page,
            page_size: 20,
            deal_names: this.data.search.deal_names,
            sales_uin: this.data.salerList[this.data.salerIdx].sales_uin,
            owner_uin: this.data.clientUin,
            type: this.data.currentType,
            start_time: this.data.search.start_time + ' 00:00:00',
            end_time: this.data.search.end_time + ' 23:59:59'
        };
        app.server.getConnect(data).then(function (res) {
            if (that.data.currentType === 1) {
                var temp = that.data.orderList;
                temp.push.apply(temp, res.data);
                that.setData({
                    orderList: temp
                })
            } else {
                var temp = that.data.newOrderList;
                temp.push.apply(temp, res.data);
                that.setData({
                    newOrderList: temp
                })
            }
            wx.hideLoading();
        }, function (res) {
            console.log(res);
            wx.hideLoading();
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        app.pageOnloadInit(this, app);
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        app.pageOnloadInit(this, app);
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.data.page++;
        this.loadData();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})